package com.manage.service.impl;

import com.manage.dao.UsersDao;
import com.manage.dao.impl.UsersDaoImpl;
import com.manage.pojo.ResultData;
import com.manage.pojo.Users;
import com.manage.service.UsersService;

import java.sql.SQLException;

public class UsersServiceImpl implements UsersService {
    @Override
    public ResultData loginCheck(String username, String password,String verifyCode,String text,String rememberMe) {
        if (!verifyCode.equalsIgnoreCase(text)){
            return  new ResultData(10000,"验证码错误");
        }
        UsersDao userDao=new UsersDaoImpl();
        try {
            Users user = userDao.findOne(username);
            if (user!=null){
                if (user.getPassword().equals(password)){
                    return new ResultData(0,"登陆成功",user);
                }else {
                    return new ResultData(10000,"密码错误");
                }
            }else {
                return new ResultData(10000,"用户不存在");
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            return new ResultData(10000,"登录验证异常");
        }
    }

    @Override
    public ResultData registerCheck(String username, String password, String verifyCode, String text) {
        if (!verifyCode.equalsIgnoreCase(text)){
            return  new ResultData(10000,"验证码错误");
        }
        UsersDao userDao=new UsersDaoImpl();
        try {
            Users one = userDao.findOne(username);
            if (one!=null){
                return new ResultData(10000,"用户名已被注册");
            }
            int i= userDao.addOne(username, password);
            if (i==1){
                return new ResultData(0,"注册成功");
            }
            return new ResultData(10000,"注册失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return new ResultData(10000,"注册异常");
        }
    }
}
